<?php


namespace App\Http\Controllers;


use App\Models\Business;
use App\Models\Pet;
use App\Models\ServicePersonnel;
use App\Models\ServiceProject;
use Illuminate\Support\Facades\Auth;

class BusinessController extends Controller
{


    public function add()
    {
        $data = request()->post();
        $business = new Business();
        $business->uid = Auth::id();
        $business->start_date = explode(',', $data['start_date']);
        $business->service_address = $data['service_address'];
        $business->service_address_desc = $data['service_address_desc'];
        $business->handover = $data['handover'];
        $business->handover_other = $data['other'];
        $business->pet_ids = explode(',', $data['pet_ids']);
        $business->service_project_ids = explode(',', $data['service_project_ids']);
        $business->service_project_needs = json_decode($data['needs'], true);
        $business->personnel_id = $data['personnel_id'];
        $business->money = $data['money'];
        $business->save();
        return Json(1, 'ok', [
            'id' => $business->id,
        ]);
    }


    public function lists()
    {
        $result = Business::query()->where('uid', '=', \auth()->id())->orderByDesc('id')->paginate();

        $pet_ids_arr = [];
        $service_project_ids_arr = [];

        foreach ($result->items() as $item) {
            array_push($pet_ids_arr, ...$item['pet_ids']);
            array_push($service_project_ids_arr, ...$item['service_project_ids']);
        }
        $pet_ids_arr = array_unique($pet_ids_arr);

        $pets = Pet::query()->select(['id', 'name'])->whereIn('id', $pet_ids_arr)->get()->toArray();

        $pets = array_column($pets, null, 'id');
        $project = ServiceProject::query()->select(['id', 'title', 'desc'])->get()->toArray();
        $projectKey = array_column($project, null, 'id');


        $personnels = ServicePersonnel::query()->select(['id', 'name','work_qr'])->whereIn('id',$service_project_ids_arr)->get()->toArray();
        $personnels = array_column($personnels, null, 'id');

        foreach ($result->items() as &$item) {
            $pets_name = [];
            $service_projects = [];
            foreach ($item['pet_ids'] as $value) {
                if (isset($pets[$value])) {
                    array_push($pets_name, $pets[$value]['name']);
                }
            }
            foreach ($item['service_project_ids'] as $value) {
                if (isset($projectKey[$value])) {
                    array_push($service_projects, $projectKey[$value]);
                }
            }

//            $time = explode(' ', $item['start_date']);

//            $item['start_date'] =  $item['start_date'];
            $item['start_time'] = '';
            $item['personnel'] = $personnels[$item['personnel_id']]['name'];
            $item['service_personnel'] = $personnels[$item['personnel_id']];
            $item['pets_name'] = implode(',', $pets_name);
            $item['service_projects'] = $service_projects;
//            array_push($pet_ids_arr,...$item['pet_ids']);
        }
        return Json(1, 'ok', $result);
    }

    public function get($id)
    {
        $business = Business::query()->where('id', '=', $id)->first();
        if(empty($business)){
            echo "订单不存在";
            exit;
        }
        $pets = Pet::query()->select(['id', 'name'])->whereIn('id', $business['pet_ids'])->get()->toArray();

        $project = ServiceProject::query()->select(['id', 'title', 'desc'])->get()->toArray();
        $projectKey = array_column($project, null, 'id');

        $personnel = ServicePersonnel::query()->select(['id', 'name','work_qr'])->where('id', '=', $business['personnel_id'])->first()->toArray();
        $business["pets_name"]=implode(',', array_column($pets,'name'));
        $business["personnel"]=$personnel['name'];

        $service_projects = [];
        foreach ($business['service_project_ids'] as $value) {
            if (isset($projectKey[$value])) {
                array_push($service_projects, $projectKey[$value]);
            }
        }
        $business["service_projects"]=$service_projects;

        return Json(1, 'ok', $business);

    }

    public function show($id){
        $data=$this->get($id)->getData()->data;
        return view('business',json_decode(json_encode($data),true));
    }

}
